package com.csxy.chy.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.chy.entity.EduSubject;
import com.csxy.chy.entity.excel.SubjectData;
import com.csxy.chy.entity.vo.OneSubject;
import com.csxy.chy.entity.vo.TwoSubject;
import com.csxy.chy.exception.MyException;
import com.csxy.chy.listener.SubjectExcelListener;
import com.csxy.chy.mapper.EduSubjectMapper;
import com.csxy.chy.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // 查询所有一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectsList = baseMapper.selectList(wrapperOne);

        // 查询所有二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);

        // 最终返回的树形结构集合
        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每一个一级分类对象，获取每一个一级分类对象值
        //封装到要求的最终list集合中
        for (int i = 0; i < oneSubjectsList.size(); i++) {//遍历oneSubjectList集合
            // 得到oneSubjectList集合，每个EduSubject对象
            EduSubject eduSubject = oneSubjectsList.get(i);
            //把eduSubject里面的值获取出来，放到oneSubject中去
            OneSubject oneSubject = new OneSubject();
            //把eduSubject数据复制到oneSubject对象中去
            BeanUtils.copyProperties(eduSubject, oneSubject);
            //多个OneSubject放到finalSubjectList里面
            finalSubjectList.add(oneSubject);

            //在一级分类循环遍历查询所有二级分类
            //创建list集合封装每个一级分类的二级分类
            ArrayList<TwoSubject> finalTwoSubjects = new ArrayList<>();

            //遍历二级list集合
            for (int j = 0; j < twoSubjectsList.size(); j++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectsList.get(j);
                //判断二级分类parentid和一级分类id是否一样
                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    finalTwoSubjects.add(twoSubject);
                }
            }
            oneSubject.setChildren(finalTwoSubjects);
        }
        return finalSubjectList;
    }

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try{
            // 文件输入流
            InputStream is = file.getInputStream();
            // 调用方法进行读取
            EasyExcel.read(is , SubjectData.class, (ReadListener) new SubjectExcelListener(eduSubjectService))
                    .sheet().doRead();
        }catch (Exception e){
            throw new MyException(20001,"excel文件读取异常！！");
        }
    }
}
